using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;
namespace SimpleGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //The physics simulator is always needed. We supply a gravity of 250 units
        PhysicsSimulator simulator = new PhysicsSimulator(new Vector2(0, 250));

        //All that is needed to create and draw a rectangle
        Body rectangleBody;
        Geom rectangleGeom;
        Texture2D rectangleTexture;
        Vector2 rectangleOrigin;

        //All that is needed to create and draw a circle
        Body circleBody;
        Geom circleGeom;
        Texture2D circleTexture;
        Vector2 circleOrigin;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Load the rectangle texture
            rectangleTexture = Content.Load<Texture2D>("Ground");
            rectangleOrigin = new Vector2(rectangleTexture.Width / 2, rectangleTexture.Height / 2);

            //Load the rectangle body
            rectangleBody = BodyFactory.Instance.CreateRectangleBody(rectangleTexture.Height, rectangleTexture.Width, 1);
            rectangleBody.Position = new Vector2(400, 500);
            
            //Make sure it does not move
            rectangleBody.IsStatic = true;
            simulator.Add(rectangleBody);

            //Load the rectangle geometry
            rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(rectangleBody, rectangleTexture.Width, rectangleTexture.Height);
            
            //Give it some bounce
            rectangleGeom.RestitutionCoefficient = 0.3f;
            
            //Give it some friction
            rectangleGeom.FrictionCoefficient = 0.5f;

            //Add it to the physics simulator
            simulator.Add(rectangleGeom);


            //Load the circle texture
            circleTexture = Content.Load<Texture2D>("Circle");
            circleOrigin = new Vector2(circleTexture.Width / 2, circleTexture.Height / 2);

            //Create the circle body
            circleBody = BodyFactory.Instance.CreateCircleBody(circleTexture.Width / 2, 1);
            circleBody.Position = new Vector2(400, 300);
            simulator.Add(circleBody);

            //Create the circle geometry
            circleGeom = GeomFactory.Instance.CreateCircleGeom(circleBody, circleTexture.Width / 2, 48);
            circleGeom.RestitutionCoefficient = 0.3f;
            circleGeom.FrictionCoefficient = 0.5f;
            simulator.Add(circleGeom);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState s = Keyboard.GetState();

            //We make it possible to rotate the circle body
            if (s.IsKeyDown(Keys.A))
                circleBody.ApplyTorque(-1000);

            if (s.IsKeyDown(Keys.D))
                circleBody.ApplyTorque(1000);

            //We update the physics simulator
            simulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            //We draw the two textures where the physics geometries are
            spriteBatch.Draw(rectangleTexture, rectangleGeom.Position, null, Color.White, rectangleGeom.Rotation, rectangleOrigin, 1, SpriteEffects.None, 1);
            spriteBatch.Draw(circleTexture, circleGeom.Position, null, Color.White, circleGeom.Rotation, circleOrigin, 1, SpriteEffects.None, 1);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
